package cn.gok.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Scanner;

public class 反射获取不同类的实例对象 {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        运行期获取实例对象();
    }

    private static void 运行期获取实例对象() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        System.out.println("请输入你要实例化对象的类名:");
        String cname = new Scanner(System.in).nextLine();
        Class<?> c = Class.forName("cn.gok.reflect." + cname);
        A a = (A) c.newInstance();
        a.fly();
        Constructor<?> con = c.getConstructor(int.class);
        A a2 = (A) con.newInstance(100);
        System.out.println(a2.getX());
    }
}
